import httplib
import urllib
import json
import hashlib
import hmac
import common
import info
from keyhandler import KeyHandler

kh = None

def market():
	good_pairs = {}
	graph = {}
	def __init__(self,gps):
		good_pairs = gps
		for k in gps:
			if k not in graph:graph[k] = []
			for v in gps[k]:
				if v not in graph:graph[v] = []
				graph[k].append(v)
				graph[v].append(k)
	def getWeight(self):
		pass
	
	def getPath(self):
		pass
	def getAllPath(self):
		pass

def getDepth(type1, type2):
	return common.makeJSONRequest("btc-e.com", "/api/2/%s_%s/depth" % (type1, type2))

def tapi(params = {}):
	global kh
	if kh is None:kh = KeyHandler("keys")
	key = str(kh.key[u'key'])
	secret = str(kh.key[u'secret'])
	nonce = kh.key[u"nonce"]
	params["nonce"] = nonce
	encoded_params = urllib.urlencode(params)

	H = hmac.new(secret, digestmod = hashlib.sha512)
	H.update(encoded_params)
	sign = H.hexdigest()
#	print "%r\n%r\n%r" % (key,secret,sign)
	
	headers = {"Key":key,"Sign":sign}
#	print headers,params
	result = common.makeJSONRequest("btc-e.com", "/tapi", headers, encoded_params)
	kh.key[u"nonce"]+=1
	kh.save()
	return result


def getInfo():
	params = {"method":"getInfo"}
	ret = tapi(params)
	return ret

def OrderList():
	"""
	when Empty:
		return {u'success': 0, u'error': u'no orders'}
	"""

	params = {"method":"OrderList", "active":1, "from_id":10245203}
	ret = tapi(params)
	return ret


if __name__ == "__main__":
	print OrderList()
	
